<template>
  <div class="parent">
    <div class="title">
      告警模板
    </div>
    <div class="tagAndSelect">
      <div class="search">
        <el-input @input="searchInputChange()" v-model="input" style="width: 240px" placeholder="搜索" />
      </div>
    </div>
    <div class="title2">
      模型
    </div>
    <div class="cards">
      <ElRow :gutter="20">
        <ElCol :xs="24" :sm="12" :md="6">
          <div class="card first">
            <div class="plusBut" @click.stop="saveOrUpdate(-1)">
              <el-icon style="font-size:30px;">
                <Plus />
              </el-icon>
              <div class="plusButFont">点击添加告警模型</div>
            </div>
          </div>
        </ElCol>
        <ElCol :xs="24" :sm="12" :md="6" v-for="value in listData">
          <div class="card second">
            <div style="padding: 20px;">
              <div class="title">{{ value.name }}（{{ value.modelNo }}）</div>
              <div class="time">{{ value.createTime }}</div>
              <div class="detailRow">
                <div class="detail">
                  模板编号<text>{{ ' ' + value.modelNo }} </text>
                </div>
                <div class="detail">
                  通知类型
                  <text v-if="value.notifyType=='1'"> {{ ' ' + '阿里云短信' }}</text>
                  <text v-if="value.notifyType=='2'"> {{ ' ' + '邮箱' }}</text>
                  <text v-if="value.notifyType=='3'"> {{ ' ' + '钉钉' }}</text>
                  <text v-if="value.notifyType=='4'"> {{ ' ' + '公众号' }}</text>
                  <text v-if="value.notifyType=='5'"> {{ ' ' + 'http' }}</text>
                </div>
              </div>
              <!-- <div class="detailRow">
                <div class="detail">
                  通知类型<text> 12</text>
                </div>
                <div class="detail">
                  入参数量<text> 12</text>
                </div>
              </div> -->
              <div class="buttom">
                <el-button type="primary" @click.stop="saveOrUpdate(value.id)">修改</el-button>
                <el-popconfirm class="box-item" title="确定要删除吗" placement="top" @confirm="del(value.id)">
                  <template #reference>
                    <el-button>删除</el-button>
                  </template>
                </el-popconfirm>
              </div>
            </div>
          </div>
        </ElCol>
      </ElRow>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { router } from '@/router'
import { BmslzService } from '@/api/bmslzApi'
import { ElMessage } from 'element-plus'

const input = ref('')
const listData = ref<any[]>([]) // 模型数组
let timeoutId: any | null = null;


onMounted(async () => {
  //页面加载初始化模型数据
  loadList();
});
//重新加载列表数据
const loadList = async () => {
  const data = await BmslzService.alarmModelList({
    pageNo: 1,
    pageSize: 999,
    name: input.value,
    modelNo: ''
  })
  listData.value = data.records;
}
const del = async (id: any) => {
  const data = await BmslzService.alarmModelDel({
    id
  })
  ElMessage.success('操作成功！');
  loadList();
}
const searchInputChange = () => {
  // 清除之前的定时器
  if (timeoutId) {
    clearTimeout(timeoutId);
  }

  // 设置新的定时器
  timeoutId = setTimeout(() => {
    loadList();
    timeoutId = null;
  }, 500);
}

const saveOrUpdate = (item: any) => {
  if (item && item != -1) {
    router.push({
      path: "/monitor-config/alarm-config-save",
      query: {
        id: item
      }
    })
  } else {
    router.push({
      path: "/monitor-config/alarm-config-save",
    })
  }
}
</script>

<style lang="scss" scoped>
.parent {
  padding: 0 0 0 20px;

  .title {
    color: #101010;
    font-size: 20px;
    font-weight: bold;
  }

  .title2 {
    color: #101010;
    font-size: 15px;
    font-weight: bold;
  }
}

.tagAndSelect {
  display: flex;
  padding-top: 10px;
  padding-bottom: 10px;
  justify-content: space-between;
  flex-wrap: wrap;

}

.tagDiv {
  display: flex;
  flex-wrap: wrap;
  align-items: center;

  .tag {
    display: flex;
    justify-content: center;
    /* 水平居中 */
    align-items: center;
    /* 垂直居中 */
    border-radius: 20px;
    margin: 0 10px 0 0;
    width: 70px;
    height: 30px;
    cursor: pointer;
    transition: background 0.3s, color 0.3s;
    font-size: 14px;
  }

  /* Hover 状态 */
  .tag:hover {
    background: #F2F3F8;
    color: #5d87ff;
  }

  .select {
    background: #F2F3F8;
    color: #5d87ff;
  }
}

.cards {
  padding-top: 20px;

  .first {
    display: flex;
    justify-content: center;
    align-items: center;

    .plusBut {
      height: 60px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;
      cursor: pointer;
      transition: background 0.3s, color 0.3s;

      .plusButFont {
        font-size: 13px;
      }
    }

    .plusBut:hover {
      color: #5d87ff;
    }
  }

  .second {
    position: relative;

    .title {
      color: #101010;
      font-size: 15px;
      font-weight: bold;
    }

    .time {
      font-size: 12px;
      padding-top: 10px;
      padding-bottom: 10px;
    }

    .detailRow {
      display: flex;
      justify-content: space-between;

      .detail {
        font-size: 13px;
        padding-top: 10px;

        text {
          color: #5d87ff;
        }
      }
    }

    .buttom {
      position: absolute;
      bottom: 10px;
      right: 10px;
    }
  }

  .card {

    border: 1px solid rgba(180, 180, 180, 0.236);
    height: 190px;
    background: #FFFFFF;
    border-radius: 10px;
    margin-bottom: 20px;
  }

}
</style>
